<template>
	<view class="userEditor">
		<uni-forms :modelValue="user" border>
			<uni-forms-item label="姓名">
				<uni-easyinput type="text" v-model="user.name" placeholder="请输入姓名" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="职务">
				<uni-data-picker :localdata="items" popup-title="请选择职务" v-model="user.type"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item label="电话">
				<uni-easyinput type="text" v-model="user.phone" placeholder="请输入电话" :inputBorder="false" />
			</uni-forms-item>
		</uni-forms>
		<view class="buttonWrap">
			<button type="primary" size="mini" @click="handleSubmit">提 交</button>
			<button size="mini" @click="handleCancel">取 消</button>
		</view>
	</view>
</template>

<script>
	const warehouse = {
		"6": ["管理者", "业务", "财务", "配送", "专卖店", "仓储", "配送负责人"],
		"0": ["业务负责人", "业务主管", "业务"],
		"1": ["财务负责人", "财务-入库", "财务"],
		"3": ["物流负责人", "仓储", "配送"],
		"4": ["专卖店负责人", "专卖店"],
	}
	export default {
		name: "userEditor",
		data() {
			return {
				user: {
					name: "",
					type: "",
					phone: "",
				},
				phoneBefore: "",
				items: []
			}
		},
		onLoad(options) {
			this.user = JSON.parse(options.user)
			this.phoneBefore = this.user.phone

			this.items = warehouse[this.userType].map(item => {
				const obj = {}
				obj.text = obj.value = item
				return obj
			})
		},
		methods: {
			async handleSubmit() {
				try {
					await this.request("userUpdate", {
						nameAfter: this.user.name,
						phoneBefore: this.phoneBefore,
						phoneAfter: this.user.phone,
						typeAfter: this.user.type,
					})

					uni.showModal({
						content: "编辑成功",
						showCancel: false,
						success(res) {
							if (res.confirm) {
								uni.navigateBack()
							}
						}
					})
				} catch (e) {
					console.log(e)
				}
			},
			handleCancel() {
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.userEditor {
		padding: 30rpx;
	}
</style>
